package org.mindrive.system.service.expenses;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import com.drew.lang.DateUtil;
import org.mindrive.system.collection.expenses.ApprovalEntity;
import org.mindrive.system.collection.expenses.DetailEntity;
import org.mindrive.system.collection.system.RoleEntity;
import org.mindrive.system.collection.system.UserEntity;
import org.mindrive.system.collection.system.UserRoleEntity;
import org.mindrive.system.core.MindriveException;
import org.mindrive.system.dto.BankDetailDTO;
import org.mindrive.system.parameter.expenses.*;
import org.mindrive.system.dto.ApprovalDTO;
import org.mindrive.system.service.BaseService;
import org.mindrive.system.util.TemplateExcelUtils;
import org.mindrive.system.util.DateTimeUtil;
import org.springframework.beans.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static org.mindrive.system.util.DateTimeUtil.YEAR_MONTH_FORMAT;

/**
 * (Approval)表服务接口
 *
 * @author maweihong
 * @since 2023-10-30 14:22:27
 */
@Service
@Slf4j
public class ApprovalService extends BaseService {

    /**
     * 查询(可分页)
     *
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @Author mwh
     * @Date 13:48 2023/8/12
     * @Param [parameter]
     **/
    public Map<String, Object> search(ApprovalSearchParameter parameter) {
        UserEntity loginUser = getLoginUser();
        QueryWrapper<ApprovalEntity> query = new QueryWrapper<ApprovalEntity>();
        query.eq("status", "0");

        if (loginUser.getUserId().equals(USER_ID_SYSTEM)){
            query.eq("DIRECTOR_RESULT", "通过");
        }

        if (parameter.getReputation() != null && !"".equals(parameter.getReputation())) {
            query.eq("REPUTATION", parameter.getReputation());
        }

//        if (parameter.getFormsTime() != null && !"".equals(parameter.getFormsTime())) {
//            query.eq("FORMS_TIME", parameter.getFormsTime());
//        }
        final UserEntity user = userDao.getByUserId(loginUser.getUserId());
        if (!user.getUserId().equals(USER_ID_SYSTEM)){
            //通过用户查询角色
            QueryWrapper<UserRoleEntity> userRoleQuery = new QueryWrapper<>();
            userRoleQuery.eq("USER_ID" , user.getId());
            final List<UserRoleEntity> list = userRoleDao.list(userRoleQuery);
            if (list.size() > 0){
                RoleEntity role = roleDao.getById(list.get(0).getRoleId());
                query.eq("REPORTING_ROLE_ID", role.getId());
            }
        }

        if (parameter.getRoleName() != null && !"".equals(parameter.getRoleName())){
            final List<RoleEntity> roleList = roleDao.list(new QueryWrapper<RoleEntity>().like("NAME", parameter.getRoleName()));
            List<String> roleIds = roleList.stream().map(RoleEntity::getId).collect(Collectors.toList());
            query.in("REPORTING_ROLE_ID" , roleIds);
        }

        if (parameter.getFormsTime() != null && !"".equals(parameter.getFormsTime())){
//            final String[] timeStr = parameter.getFormsTime().split("-");
//            query.apply("YEAR(FORMS_TIME) = "+Integer.parseInt(timeStr[0]));
//            query.apply("MONTH(FORMS_TIME) = "+Integer.parseInt(timeStr[1]));
            query.apply("formatdatetime(FORMS_TIME , 'yyyy-MM') = '"+parameter.getFormsTime()+"'");
        }
        //科室主任id
//        if (parameter.getDirector() != null && !"".equals(parameter.getDirector())) {
//            query.eq("DIRECTOR", parameter.getDirector());
//        }

        //填报角色id
//        if (parameter.getReportingRoleId() != null && !"".equals(parameter.getReportingRoleId())) {
//            query.eq("REPORTING_ROLE_ID", parameter.getReportingRoleId());
//        }
//
//        if (parameter.getDirectorResult() != null && !"".equals(parameter.getDirectorResult())) {
//            query.eq("DIRECTOR_RESULT", parameter.getDirectorResult());
//        }
//
//        if (parameter.getDirectorFeedback() != null && !"".equals(parameter.getDirectorFeedback())) {
//            query.eq("DIRECTOR_FEEDBACK", parameter.getDirectorFeedback());
//        }
//
//        if (parameter.getFinanceId() != null && !"".equals(parameter.getFinanceId())) {
//            query.eq("FINANCE_ID", parameter.getFinanceId());
//        }
//
//        if (parameter.getFinanceResult() != null && !"".equals(parameter.getFinanceResult())) {
//            query.eq("FINANCE_RESULT", parameter.getFinanceResult());
//        }
//
//        if (parameter.getFinanceFeedback() != null && !"".equals(parameter.getFinanceFeedback())) {
//            query.eq("FINANCE_FEEDBACK", parameter.getFinanceFeedback());
//        }

        int count = (int) approvalDao.count(query);
        query.orderByAsc(CREATED_AT);

        List<ApprovalEntity> list = new ArrayList<ApprovalEntity>();
        List<ApprovalDTO> listDto = new ArrayList<ApprovalDTO>();
        Page<ApprovalEntity> page = page(ApprovalEntity.class, parameter.getPageSize(), parameter.getCurrentPage());
        if (page == null) {
            list = approvalDao.list(query);
        } else {
            approvalDao.page(page, query);
            list = page.getRecords();
        }

        for (ApprovalEntity approval : list) {
            ApprovalDTO approvalDTO = new ApprovalDTO();
            BeanUtils.copyProperties(approval, approvalDTO);
            listDto.add(approvalDTO);
        }

        Map<String, Object> result = new HashMap<String, Object>();
        result.put(COUNT, count);
        result.put(LIST, listDto);
        return result;
    }

    public ApprovalEntity detail(String id) {
        return approvalDao.getById(id);
    }

    public ApprovalEntity create(ApprovalParameter parameter) throws ParseException {
        final UserEntity loginUser = getLoginUser();
        final UserEntity user = userDao.getByUserId(loginUser.getUserId());
        ApprovalEntity approval = new ApprovalEntity();
        BeanUtils.copyProperties(parameter, approval);
        approval.setFormsTime(DateTimeUtil.parse(parameter.getFormsTime(), YEAR_MONTH_FORMAT));
        approval.setStatus("0");

        approval.setDirector(user.getId());
        QueryWrapper<UserRoleEntity> userRoleQuery = new QueryWrapper<>();
        userRoleQuery.eq("USER_ID" , user.getId());
        final List<UserRoleEntity> list = userRoleDao.list(userRoleQuery);
        if (list.size()>0){
            final RoleEntity role = roleDao.getById(list.get(0).getRoleId());
            approval.setReportingRoleId(role.getId());
        }else {
            throw new MindriveException("当前用户无所在科室,无法创建报表");
        }
        approval.setDirectorResult("未审核");
        approval.setFinanceResult("未审核");
        approval.setCreator(loginUser.getUserId());
        approval.setCreateAt(new Date());
        approvalDao.save(approval);

        //根据填报人判断当前为哪个科室,批量生成对应科室人员数据
        final List<UserRoleEntity> users = userRoleDao.list(new QueryWrapper<UserRoleEntity>().eq("USER_ID", user.getId()));
        if (users.size()>0){
            final RoleEntity role = roleDao.getById(users.get(0).getRoleId());
            final List<UserRoleEntity> roleList = userRoleDao.list(new QueryWrapper<UserRoleEntity>().eq("ROLE_ID", role.getId()));
            for (UserRoleEntity userRole : roleList){
                DetailEntity detail = new DetailEntity();
                detail.setApprovalId(approval.getId());
//                detail.setDetailOrder();
                detail.setUserId(userRole.getUserId()) ;
                detail.setDetailName(userDao.getById(userRole.getUserId()).getUserName());
                detail.setCaseHandlersWorkingStandards(70.0);
                detail.setCaseHandlersWorkingStandardsDays("0");
                detail.setCaseHandlersWorkingStandardsSubtotal(0.0);
                detail.setCaseHandlersHolidays(140.0);
                detail.setCaseHandlersHolidaysDays("0");
                detail.setCaseHandlersHolidaysSubtotal(0.0);
                detail.setAuxiliaryCaseHandlersWorkingStandards(55.0);
                detail.setAuxiliaryCaseHandlersWorkingStandardsDays("0");
                detail.setAuxiliaryCaseHandlersWorkingStandardsSubtotal(0.0);
                detail.setAuxiliaryCaseHandlersHolidays(110.0);
                detail.setAuxiliaryCaseHandlersHolidaysDays("0");
                detail.setAuxiliaryCaseHandlersHolidaysSubtotal(0.0);
                detail.setTotal(0.0);
                detail.setCommunicationExpenses(0.0);
                detail.setSumOfMoney(0.0);
                detailDao.save(detail);
            }
        }
        return approval;
    }

    public void modify(String id, ApprovalParameter parameter) {
        final UserEntity loginUser = getLoginUser();
        ApprovalEntity approval = new ApprovalEntity();
        BeanUtils.copyProperties(parameter, approval);
        approval.setId(id);
        approval.setUpdator(loginUser.getUserName());
        approval.setUpdateTime(new Date());
        approvalDao.updateById(approval);
    }

    public void delete(List<String> ids) {
        final UserEntity loginUser = getLoginUser();
        for (String id : ids) {
            ApprovalEntity obj = approvalDao.getById(id);
            obj.setStatus("1");
            obj.setUpdator(loginUser.getUserName());
            obj.setUpdateTime(new Date());
            approvalDao.updateById(obj);
            detailDao.remove(new QueryWrapper<DetailEntity>().eq("APPROVAL_ID" , id));
        }
    }

    public void deletePhysics(List<String> ids) {
        approvalDao.removeByIds(ids);
    }

    public void detailInsertgai(List<List<Map<String , Object>>> parameter){
        final UserEntity loginUser = getLoginUser();

        String approvalId = "";
        List<DetailEntity> detailEntityList = new ArrayList<>();
        for (List<Map<String , Object>> hangList : parameter){
            DetailEntity detail = new DetailEntity();
            for (Map<String , Object> value : hangList){
                switch (String.valueOf(value.get("name"))) {
                    case "0":
                        //序号
                        System.out.println();
                        continue;
                    case "1":
                        //姓名
                        detail.setDetailName(String.valueOf(value.get("value")));
                        continue;
                    case "2":
                        //办案人员工作日标准
                        detail.setCaseHandlersWorkingStandards(Double.parseDouble(value.get("value")+""));
                        continue;
                    case "3":
                        //办案人员工作日标准天数
                        detail.setCaseHandlersWorkingStandardsDays(String.valueOf(value.get("value")));
                        continue;
                    case "4":
                        //办案人员工作日标准小计
                        detail.setCaseHandlersWorkingStandardsSubtotal(Double.parseDouble(String.valueOf(value.get("value"))));
                        continue;
                    case "5":
                        //办案人员公休节假日标准
                        detail.setCaseHandlersHolidays(Double.parseDouble(String.valueOf(value.get("value"))));
                        continue;
                    case "6":
                        //办案人员公休节假日标准天数
                        detail.setCaseHandlersHolidaysDays(String.valueOf(value.get("value")));
                        continue;
                    case "7":
                        //办案人员公休节假日标准小计
                        detail.setCaseHandlersHolidaysSubtotal(Double.parseDouble(String.valueOf(value.get("value"))));
                        continue;
                    case "8":
                        //辅助办案人员工作日标准
                        detail.setAuxiliaryCaseHandlersWorkingStandards(Double.parseDouble(String.valueOf(value.get("value"))));
                        continue;
                    case "9":
                        //辅助办案人员工作日标准天数
                        detail.setAuxiliaryCaseHandlersWorkingStandardsDays(String.valueOf(value.get("value")));
                        continue;
                    case "10":
                        //辅助办案人员工作日小计
                        detail.setAuxiliaryCaseHandlersWorkingStandardsSubtotal(Double.parseDouble(String.valueOf(value.get("value"))));
                        continue;
                    case "11":
                        //辅助办案人员公休日节假日标准
                        detail.setAuxiliaryCaseHandlersHolidays(Double.parseDouble(String.valueOf(value.get("value"))));
                        continue;
                    case "12":
                        //辅助办案人员公休日节假日标准天数
                        detail.setAuxiliaryCaseHandlersHolidaysDays(String.valueOf(value.get("value")));
                        continue;
                    case "13":
                        //辅助办案人员公休日节假日标准小计
                        detail.setAuxiliaryCaseHandlersHolidaysSubtotal(Double.parseDouble(String.valueOf(value.get("value"))));
                        continue;
                    case "14":
                        //合计
                        detail.setTotal(Double.parseDouble(String.valueOf(value.get("value"))));
                        continue;
                    case "15":
                        //通讯费
                        detail.setCommunicationExpenses(Double.parseDouble(String.valueOf(value.get("value"))));
                        continue;
                    case "16":
                        //领取金额
                        detail.setSumOfMoney(Double.parseDouble(String.valueOf(value.get("value"))));
                        continue;
                    case "17":
                        //备注
                        if (!value.get("value").equals("0")){
                            detail.setRemake(String.valueOf(value.get("value")));
                        }
                        continue;
                    case "18":
                        approvalId = String.valueOf(value.get("value"));
                        continue;
                    case "19":
                        detail.setUserId(String.valueOf(value.get("value")));
                        continue;
                }
            }
            detail.setCreator(loginUser.getUserId());
            detail.setCreateAt(new Date());
            detail.setApprovalId(approvalId);

            detailEntityList.add(detail);
        }
        //删除原来的值
        QueryWrapper<DetailEntity> detailQuery = new QueryWrapper<>();
        detailQuery.eq("APPROVAL_ID", approvalId);
        detailDao.remove(detailQuery);

        for (DetailEntity detail : detailEntityList){
            detailDao.save(detail);
            //修改user的fillOutStatus
            UpdateWrapper<UserEntity> updateWrapper = new UpdateWrapper<UserEntity>()
                    .eq("id", loginUser.getId()).set("FILL_OUT_STATUS", "1");
            userDao.update(updateWrapper);
        }

    }

    public DetailEntity detailInsert(DetailParameter parameter) {
        final UserEntity loginUser = getLoginUser();
        DetailEntity detailEntity = new DetailEntity();
        BeanUtils.copyProperties(parameter, detailEntity);

        Double total1 = 0.0;
        Double total2 = 0.0;
        Double total3 = 0.0;
        Double total4 = 0.0;
        Double total5 = 0.0;
        //办案人员工作日标准小计
        if (parameter.getCaseHandlersWorkingStandardsDays() != null && !"".equals(parameter.getCaseHandlersWorkingStandardsDays())) {
            total1 = Double.parseDouble(parameter.getCaseHandlersWorkingStandardsDays()) * parameter.getCaseHandlersWorkingStandards();
            detailEntity.setCaseHandlersWorkingStandardsSubtotal(total1);
        } else {
            detailEntity.setCaseHandlersWorkingStandardsDays("0");
            detailEntity.setCaseHandlersWorkingStandardsSubtotal(0.0);
        }
        //办案人员公休节假日标准小计
        if (parameter.getCaseHandlersHolidaysDays() != null && !"".equals(parameter.getCaseHandlersHolidaysDays())) {
            total2 = Double.parseDouble(parameter.getCaseHandlersHolidaysDays()) * parameter.getCaseHandlersHolidays();
            detailEntity.setCaseHandlersHolidaysSubtotal(total2);
        } else {
            detailEntity.setCaseHandlersHolidaysDays("0");
            detailEntity.setCaseHandlersHolidaysSubtotal(0.0);
        }
        //辅助办案人员工作日小计
        if (parameter.getAuxiliaryCaseHandlersWorkingStandardsDays() != null && !"".equals(parameter.getAuxiliaryCaseHandlersWorkingStandardsDays())) {
            total3 = Double.parseDouble(parameter.getAuxiliaryCaseHandlersWorkingStandardsDays()) * parameter.getAuxiliaryCaseHandlersWorkingStandards();
            detailEntity.setAuxiliaryCaseHandlersWorkingStandardsSubtotal(total3);
        } else {
            detailEntity.setAuxiliaryCaseHandlersWorkingStandardsDays("0");
            detailEntity.setAuxiliaryCaseHandlersWorkingStandardsSubtotal(0.0);
        }
        //辅助办案人员公休日节假日标准小计
        if (parameter.getAuxiliaryCaseHandlersHolidaysDays() != null && !"".equals(parameter.getAuxiliaryCaseHandlersHolidaysDays())) {
            total4 = Double.parseDouble(parameter.getAuxiliaryCaseHandlersHolidaysDays()) * parameter.getAuxiliaryCaseHandlersHolidays();
            detailEntity.setAuxiliaryCaseHandlersHolidaysSubtotal(total4);
        } else {
            detailEntity.setAuxiliaryCaseHandlersHolidaysDays("0");
            detailEntity.setAuxiliaryCaseHandlersHolidaysSubtotal(0.0);
        }
        //合计
        Double total = total1 + total2 + total3 + total4;
        detailEntity.setTotal(total);

        //通讯费
        if (parameter.getCommunicationExpenses() != null && !"".equals(parameter.getCommunicationExpenses())) {
            total5 = parameter.getCommunicationExpenses();
            detailEntity.setCommunicationExpenses(total5);
        } else {
            detailEntity.setCommunicationExpenses(0.0);
        }
        //领取金额
        Double sumOfMoney = total1 + total2 + total3 + total4 + total5;
        detailEntity.setSumOfMoney(sumOfMoney);

        detailEntity.setCreator(loginUser.getUserId());
        detailEntity.setCreateAt(new Date());
        detailEntity.setRemake(parameter.getRemake());
        detailDao.save(detailEntity);

        //修改user的fillOutStatus
        UpdateWrapper<UserEntity> updateWrapper = new UpdateWrapper<UserEntity>()
                .eq("id", loginUser.getId()).set("FILL_OUT_STATUS", "1");
        userDao.update(updateWrapper);

        return detailEntity;
    }

    public void directorUpdate(String id, DirectorUpdateApprovalParameter parameter) {
        final UserEntity loginUser = getLoginUser();
        ApprovalEntity approval = new ApprovalEntity();
        BeanUtils.copyProperties(parameter, approval);
        approval.setId(id);
        approval.setUpdator(loginUser.getUserName());
        approval.setUpdateTime(new Date());
        approvalDao.updateById(approval);
    }

    public void financeUpdate(String id, FinanceUpdateApprovalParameter parameter) {
        final UserEntity loginUser = getLoginUser();
        ApprovalEntity approvalEntity = approvalDao.getById(id);
        if (approvalEntity.getDirectorResult().equals("未审核") || approvalEntity.getDirectorResult().equals("未通过")) {
            throw new MindriveException("此审批单还未通过科室主任的审核");
        } else {
            ApprovalEntity approval = new ApprovalEntity();
            BeanUtils.copyProperties(parameter, approval);
            approval.setId(id);
            approval.setUpdator(loginUser.getUserName());
            approval.setUpdateTime(new Date());
            approvalDao.updateById(approval);
        }
    }

    public static int getMonthDays(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, 1);
        int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        return days;
    }


    public void expertBankDerive(ExpertParameter parameter, HttpServletResponse response) throws Exception{
        final List<BankDetailDTO> detailList = searchBankDetailList(parameter);
        try {
            Map<String, Object> param = new HashMap<>();
            param.put("title", "resultBankData");
            param.put("list", detailList);
            TemplateExcelUtils.downLoadExcel("resultBankData", "resultBankData.xlsx", param, response);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 导出报表
     *
     * @return void
     * @Author mwh
     * @Date 14:25 2023/10/9
     * @Param [file]
     **/
    public void expertDerive(String approvalId, HttpServletResponse response) throws Exception {
        List<DetailEntity> detailList = searchDetailList(approvalId);
        final ApprovalEntity approval = approvalDao.getById(approvalId);
        SimpleDateFormat yearSdf = new SimpleDateFormat("yyyy-MM-dd");
        final String[] newDate = yearSdf.format(new Date()).split("-");
        try {
            Map<String, Object> param = new HashMap<>();
            param.put("title", "resultData");
            param.put("list", detailList);
            param.put("reputation", approval.getReputation());
            int monthDays = getMonthDays(Integer.parseInt(newDate[0]), Integer.parseInt(newDate[1]));
            String formsTimeStr = newDate[0] + "." + newDate[1] + "." + "1-" + newDate[0] + "." + newDate[1] + "." + monthDays;
            param.put("formsTimeStr", formsTimeStr);
            TemplateExcelUtils.downLoadExcel("resultData", "resultData.xlsx", param, response);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public List<BankDetailDTO> searchBankDetailList(ExpertParameter parameter){
        List<DetailEntity> detailList = new ArrayList<>();
        if (parameter.getApprovalId() != null && !"".equals(parameter.getApprovalId())){
            //通过报表id查询报表详情
            QueryWrapper<DetailEntity> detailQuery = new QueryWrapper<>();
            detailQuery.eq("APPROVAL_ID", parameter.getApprovalId());
            if (parameter.getBeSummarizedUserIds()!=null && !"".equals(parameter.getBeSummarizedUserIds())){
                final String[] beSummarizedUserIds = parameter.getBeSummarizedUserIds().split(",");
                detailQuery.in("USER_ID" , beSummarizedUserIds);
            }
            detailList = detailDao.list(detailQuery);
        }else {
            final String[] timeStr = parameter.getFormsTime().split("-");
            List<ApprovalEntity> approvalEntityList = new ArrayList<>();
            if (parameter.getReputation() != null && !"".equals(parameter.getReputation())){
                approvalEntityList = approvalDao.searchApprovalByTimeAndRole(Integer.parseInt(timeStr[0]), Integer.parseInt(timeStr[1]) , parameter.getReputation());
            }else {
                approvalEntityList = approvalDao.searchApprovalByTime(Integer.parseInt(timeStr[0]), Integer.parseInt(timeStr[1]));
            }
            for (ApprovalEntity approval : approvalEntityList){
                detailList.addAll(detailDao.list(new QueryWrapper<DetailEntity>().eq("APPROVAL_ID" , approval.getId())));
            }
        }
        List<BankDetailDTO> bankDetailDTOS = new ArrayList<>();
        int sort = 1;

        //V2.5 修改为:判断是否有"金额汇总人id".若有,则将"汇总人用户id集合"中的用户金额全部累计到金额汇总人金额列中
        if (parameter.getCollectUserId()!=null && !"".equals(parameter.getCollectUserId())){
            Double collectMoney = 0.0;
            for (DetailEntity detail : detailList){
                if (!detail.getUserId().equals(parameter.getCollectUserId())){
                    collectMoney += detail.getSumOfMoney();
                    detail.setSumOfMoney(0.0);
                }
            }
            for (DetailEntity detail : detailList){
                if (detail.getUserId().equals(parameter.getCollectUserId())){
                    detail.setSumOfMoney(detail.getSumOfMoney() + collectMoney);
                }
            }
        }

        for (DetailEntity detail : detailList){
            BankDetailDTO dto = new BankDetailDTO();
            //通过用户id查询
            UserEntity user = userDao.getById(detail.getUserId());
            dto.setDetailOrder(String.valueOf(sort));
            sort++;

            if (user.getBankCardNumber() != null && !"".equals(user.getBankCardNumber())){
                dto.setBankCardNumber(user.getBankCardNumber());
            }

            final List<UserRoleEntity> users = userRoleDao.list(new QueryWrapper<UserRoleEntity>().eq("USER_ID", user.getId()));
            if (users.size()>0){
                final RoleEntity role = roleDao.getById(users.get(0).getRoleId());
                dto.setRoleName(role.getName());
            }

            dto.setDetailName(detail.getDetailName());

            //V1.0 直接根据每个用户的"总金额"情况录入金额
            if (String.valueOf(detail.getSumOfMoney()).contains(".0")){
                dto.setSumOfMoney(StringUtils.substringBefore(String.valueOf(detail.getSumOfMoney()) , "."));
            }else {
                dto.setSumOfMoney(String.valueOf(detail.getSumOfMoney()));
            }

            dto.setModuleStr(user.getBankCardNumber() + ","+detail.getDetailName() + ","+dto.getSumOfMoney()+",");
            bankDetailDTOS.add(dto);
        }
        return bankDetailDTOS;
    }

    public List<DetailEntity> searchDetailList(String approvalId) {
        QueryWrapper<DetailEntity> detailQuery = new QueryWrapper<>();
        detailQuery.eq("APPROVAL_ID", approvalId);
        final List<DetailEntity> detailList = detailDao.list(detailQuery);

        int sort = 1;
        int caseHandlersWorkingStandardsDays = 0;
        Double caseHandlersWorkingStandardsSubtotal = 0.0;

        int caseHandlersHolidaysDays = 0;
        Double caseHandlersHolidaysSubtotal = 0.0;

        int auxiliaryCaseHandlersWorkingStandardsDays = 0;
        Double auxiliaryCaseHandlersWorkingStandardsSubtotal = 0.0;

        int auxiliaryCaseHandlersHolidaysDays = 0;
        Double auxiliaryCaseHandlersHolidaysSubtotal = 0.0;

        Double total = 0.0;

        Double sumOfMoney = 0.0;

        for (DetailEntity detail : detailList) {
            detail.setDetailOrder(sort + "");
            caseHandlersWorkingStandardsDays += Integer.parseInt(detail.getCaseHandlersWorkingStandardsDays());
            caseHandlersWorkingStandardsSubtotal += detail.getCaseHandlersWorkingStandardsSubtotal();

            caseHandlersHolidaysDays += Integer.parseInt(detail.getCaseHandlersHolidaysDays());
            caseHandlersHolidaysSubtotal += detail.getCaseHandlersHolidaysSubtotal();

            auxiliaryCaseHandlersWorkingStandardsDays += Integer.parseInt(detail.getAuxiliaryCaseHandlersWorkingStandardsDays());
            auxiliaryCaseHandlersWorkingStandardsSubtotal += detail.getAuxiliaryCaseHandlersWorkingStandardsSubtotal();

            auxiliaryCaseHandlersHolidaysDays += Integer.parseInt(detail.getAuxiliaryCaseHandlersHolidaysDays());
            auxiliaryCaseHandlersHolidaysSubtotal += detail.getAuxiliaryCaseHandlersHolidaysSubtotal();

            total += detail.getTotal();

            sumOfMoney += detail.getSumOfMoney();
            sort++;
        }
        DetailEntity heJi = new DetailEntity();
        heJi.setDetailOrder("合计");
        heJi.setCaseHandlersWorkingStandardsDays(caseHandlersWorkingStandardsDays + "");
        heJi.setCaseHandlersWorkingStandardsSubtotal(caseHandlersWorkingStandardsSubtotal);

        heJi.setCaseHandlersHolidaysDays(caseHandlersHolidaysDays + "");
        heJi.setCaseHandlersHolidaysSubtotal(caseHandlersHolidaysSubtotal);

        heJi.setAuxiliaryCaseHandlersWorkingStandardsDays(auxiliaryCaseHandlersWorkingStandardsDays + "");
        heJi.setAuxiliaryCaseHandlersWorkingStandardsSubtotal(auxiliaryCaseHandlersWorkingStandardsSubtotal);

        heJi.setAuxiliaryCaseHandlersHolidaysDays(auxiliaryCaseHandlersHolidaysDays + "");
        heJi.setAuxiliaryCaseHandlersHolidaysSubtotal(auxiliaryCaseHandlersHolidaysSubtotal);

        heJi.setTotal(total);
        heJi.setSumOfMoney(sumOfMoney);

        detailList.add(heJi);
        return detailList;
    }

    public List<List<Map<String , Object>>> searchDetailListGai(String approvalId) {
        QueryWrapper<DetailEntity> detailQuery = new QueryWrapper<>();
        detailQuery.eq("APPROVAL_ID", approvalId);
        final List<DetailEntity> detailList = detailDao.list(detailQuery);
        int sort = 1;
        List<List<Map<String , Object>>> list = new ArrayList<>();
        for (DetailEntity detail : detailList) {
            List<Map<String , Object>> hang = new ArrayList<>();
            detail.setDetailOrder(sort + "");
            Map<String , Object> map0 = new HashMap<>();
            map0.put("name" , "0");
            map0.put("value" , sort);
            hang.add(map0);
            sort++;
            Map<String , Object> map1 = new HashMap<>();
            map1.put("name", "1");
            map1.put("value" , detail.getDetailName());
            hang.add(map1);

            Map<String , Object> map2 = new HashMap<>();
            map2.put("name", "2");
            map2.put("value" , detail.getCaseHandlersWorkingStandards());
            hang.add(map2);
            Map<String , Object> map3 = new HashMap<>();
            map3.put("name", "3");
            map3.put("value" , detail.getCaseHandlersWorkingStandardsDays());
            hang.add(map3);
            Map<String , Object> map4 = new HashMap<>();
            map4.put("name", "4");
            map4.put("value" , detail.getCaseHandlersWorkingStandardsSubtotal());
            hang.add(map4);

            Map<String , Object> map5 = new HashMap<>();
            map5.put("name", "5");
            map5.put("value" , detail.getCaseHandlersHolidays());
            hang.add(map5);
            Map<String , Object> map6 = new HashMap<>();
            map6.put("name", "6");
            map6.put("value" , detail.getCaseHandlersHolidaysDays());
            hang.add(map6);
            Map<String , Object> map7 = new HashMap<>();
            map7.put("name", "7");
            map7.put("value" , detail.getCaseHandlersHolidaysSubtotal());
            hang.add(map7);

            Map<String , Object> map8 = new HashMap<>();
            map8.put("name", "8");
            map8.put("value" , detail.getAuxiliaryCaseHandlersWorkingStandards());
            hang.add(map8);
            Map<String , Object> map9 = new HashMap<>();
            map9.put("name", "9");
            map9.put("value" , detail.getAuxiliaryCaseHandlersWorkingStandardsDays());
            hang.add(map9);
            Map<String , Object> map10 = new HashMap<>();
            map10.put("name", "10");
            map10.put("value" , detail.getAuxiliaryCaseHandlersWorkingStandardsSubtotal());
            hang.add(map10);
            Map<String , Object> map11 = new HashMap<>();
            map11.put("name", "11");
            map11.put("value" , detail.getAuxiliaryCaseHandlersHolidays());
            hang.add(map11);
            Map<String , Object> map12 = new HashMap<>();
            map12.put("name", "12");
            map12.put("value" , detail.getAuxiliaryCaseHandlersHolidaysDays());
            hang.add(map12);
            Map<String , Object> map13 = new HashMap<>();
            map13.put("name", "13");
            map13.put("value" , detail.getAuxiliaryCaseHandlersHolidaysSubtotal());
            hang.add(map13);
            Map<String , Object> map14 = new HashMap<>();
            map14.put("name", "14");
            map14.put("value" , detail.getTotal());
            hang.add(map14);
            Map<String , Object> map15 = new HashMap<>();
            map15.put("name", "15");
            map15.put("value" , detail.getCommunicationExpenses());
            hang.add(map15);
            Map<String , Object> map16 = new HashMap<>();
            map16.put("name", "16");
            map16.put("value" , detail.getSumOfMoney());
            hang.add(map16);
            Map<String , Object> map17 = new HashMap<>();
            map17.put("name", "17");
            map17.put("value" , detail.getRemake());
            hang.add(map17);
            Map<String , Object> map18 = new HashMap<>();
            map18.put("name", "18");
            map18.put("value" , detail.getUserId());
            hang.add(map18);
            list.add(hang);
        }
        return list;
    }

}

